package onlinecalsys.client.controller;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import onlinecalsys.client.view.View;
import onlinecalsys.client.view.ObserverCommon.Observer;
import onlinecalsys.server.controller.ServerController;
import onlinecalsys.server.objects.Appointment;

public class CalsysClientController extends UnicastRemoteObject implements Observer {

	/**
	 * 
	 */
	private ServerController sercon;
	private View view;
	

	public CalsysClientController(View view) throws RemoteException {
		super();

		try {

			if (System.getSecurityManager() == null) {

				System.setSecurityManager(new RMISecurityManager());
			}

			sercon = (ServerController) Naming.lookup(URL);
			this.view = view;

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	public boolean addAppointment(Appointment app) throws RemoteException {
		System.out.println("controller client: " + app.getShortDateRep());
		return sercon.addApointment(app);
	}

	@Override
	public ArrayList<ArrayList<String>> getAppointments(String date,
			String username) throws RemoteException {
		return sercon.getAppointments(date, username);

	}

	@Override
	public boolean deleteAppointment(int appointmentId, String username)
			throws RemoteException {
		return sercon.deleteAppointment(appointmentId, username);
	}

	@Override
	public boolean usernameChecking(String username, String password)
			throws RemoteException {
		return sercon.usernameChecking(username, password);
	}

	@Override
	public boolean saveNote(String note,String username) throws RemoteException {

		return sercon.saveNote(note,username);
	}
	@Override
	public String getNote(String username) throws RemoteException {
		
		return  sercon.getNote(username);
	}

	@Override
	public boolean shareAppointment(String username, int id,
			String shareWithUser) throws RemoteException {
		return sercon.shareAppointment(username, id, shareWithUser);
	}

	@Override
	public void update(String username) throws RemoteException {
		if(view.getUsername().equals(username)) {
			view.getSharedAppointments();
		}
	}


	@Override
	public ArrayList<ArrayList<String>> getSharedAppointments(String username) throws RemoteException {
		
			System.out.println(username);
			
				ArrayList<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
				list = sercon.getSharedAppointments(username);
				return list;			
		
	}



	@Override
	public void onlineNow() throws RemoteException {
		sercon.attach(this);
	}



	@Override
	public String getUsername() throws RemoteException {
		return view.getUsername();
	}



	@Override
	public boolean createNewUser(String username, String password) throws RemoteException {
		return sercon.createNewUser(username, password);
	}



	

}
